package com.nachsparty.keepmychange;

import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;

import com.nachsparty.keepmychange.data.Constants;
import com.nachsparty.keepmychange.data.Data;

public class ConfigActivity extends Activity {
    /** Called when the activity is first created. */
	SharedPreferences settings;
	SharedPreferences.Editor editor;
	int botonseleccionado=0;
	EditText editobjetivo;
	EditText editdni;
	EditText editpass;
	Integer objetivo;
	//int redondeo = 1;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.config);
        if(Data.login){
            RelativeLayout loginform = (RelativeLayout) findViewById(R.id.loginform);
            loginform.setVisibility(RelativeLayout.GONE);
            Button logoutbutton = (Button) findViewById(R.id.button_logout);
            logoutbutton.setVisibility(Button.VISIBLE);
        }else{
            RelativeLayout loginform = (RelativeLayout) findViewById(R.id.loginform);
            loginform.setVisibility(RelativeLayout.VISIBLE);
            Button logoutbutton = (Button) findViewById(R.id.button_logout);
            logoutbutton.setVisibility(Button.GONE);
        }
        
        
        Spinner spinner = (Spinner) findViewById(R.id.spinnerinstis);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
         
   }
    public void atrasPressed(View view) {
	//	Intent myIntent = new Intent(view.getContext(), InfoActivity.class);
	//	startActivity(myIntent);
    	finish();
	}
    public void makeButtonsOff(){
    	Button boton;
    	boton = (Button) findViewById(R.id.button1);
    	boton.setBackgroundResource(R.drawable.boton_off);
    	boton = (Button) findViewById(R.id.button2);
    	boton.setBackgroundResource(R.drawable.boton_off);
    	boton = (Button) findViewById(R.id.button3);
    	boton.setBackgroundResource(R.drawable.boton_off);
    	boton = (Button) findViewById(R.id.button4);
    	boton.setBackgroundResource(R.drawable.boton_off);
    	boton = (Button) findViewById(R.id.button5);
    	boton.setBackgroundResource(R.drawable.boton_off);
    }
    public void button1Pressed(View view) {
    	botonseleccionado = 1;
    	makeButtonsOff();
		Button boton = (Button) findViewById(R.id.button1);
		boton.setBackgroundResource(R.drawable.boton_seleccionado);
    }
    public void button2Pressed(View view) {
    	botonseleccionado = 2;
    	makeButtonsOff();
		Button boton = (Button) findViewById(R.id.button2);
		boton.setBackgroundResource(R.drawable.boton_seleccionado);
    }
    public void button3Pressed(View view) {
    	botonseleccionado = 3;
    	makeButtonsOff();
		Button boton = (Button) findViewById(R.id.button3);
		boton.setBackgroundResource(R.drawable.boton_seleccionado);
    }
    public void button4Pressed(View view) {
    	botonseleccionado = 4;
    	makeButtonsOff();
		Button boton = (Button) findViewById(R.id.button4);
		boton.setBackgroundResource(R.drawable.boton_seleccionado);
    }
    public void button5Pressed(View view) {
    	botonseleccionado = 5;
    	makeButtonsOff();
		Button boton = (Button) findViewById(R.id.button5);
		boton.setBackgroundResource(R.drawable.boton_seleccionado);
    } 
    public void buttonSavePressed(View view) {
    	
    	if(loginProcess()){
    		Data.login = true;	 
		  	objetivo = Integer.parseInt(editobjetivo.getText().toString());
		  	Data.objetivo = objetivo;
		  	Log.d("KMC","OBJETIVO: "+objetivo);
		  	Log.d("KMC", "boton: "+botonseleccionado);
		  	
    		//LOGIN
            // We need an Editor object to make preference changes.
            // All objects are from android.context.Context
    		settings = getSharedPreferences(Constants.PREFS_NAME, 0);
            editor = settings.edit();
            editor.putBoolean("logado", true);
            editor.putInt("objetivo", objetivo);
            editor.putString("dni", editdni.getText().toString());
            editor.putString("pass", editpass.getText().toString());
            // Commit the edits!
            editor.commit();
            if(KeepMyChangeActivity.sharedinstance!=null){
            	KeepMyChangeActivity.sharedinstance.finish();
            }
    		Intent myIntent = new Intent(view.getContext(), KeepMyChangeActivity.class);
    		startActivity(myIntent);
    		finish();
    		
    	}else{
    		AlertDialog.Builder builder = new AlertDialog.Builder(ConfigActivity.this);
        	builder.setTitle("Error");
        	builder.setMessage("No se pudo completar el proceso de Login, por favor, revise los campos rellenados");
        	builder.setCancelable(false)
        	.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
        		public void onClick(DialogInterface dialog, int id) {
        			dialog.cancel();
        		}
        	});
        	AlertDialog alert = builder.create();
        	alert.show();
    	}
    }
    public void buttonLogoutPressed(View view) {
    	Data.login = false;
    	//LOGOUT
        // We need an Editor object to make preference changes.
        // All objects are from android.context.Context
        settings = getSharedPreferences(Constants.PREFS_NAME, 0);
        editor = settings.edit();
        editor.putBoolean("logado", false);
        // Commit the edits!
        editor.commit();
    	
        Intent myIntent = new Intent(view.getContext(), FirstStartActivity.class);
    	startActivity(myIntent);

        finish();
    }
    public boolean loginProcess(){
    	editobjetivo = (EditText) findViewById(R.id.editText1);
		editdni = (EditText) findViewById(R.id.editText3);
		editpass = (EditText) findViewById(R.id.editText4);
		Integer objetivo = 0; 
		if(!(editobjetivo.getText().toString().equals("")) &&!(botonseleccionado == 0)){
  			 if(Data.login)return true;
  			 else if(!(editdni.getText().toString().equals(""))
	  			  &&!(editpass.getText().toString().equals("")))
	  				  return true;
  			 
		}
		else{
			return false;
		}
		return false;
		
		
    }
}